<?php
if(!defined('IN_MANU')){ die('[ERROR] You cannot load this page directly !!!'); }

/* @author     tubaduc <tubaduc@gmail.com> 
 * @copyright  (c) Pyco24
 * @since      26.07.11
 * @version    2.0
 * */

class LoginController extends BaseController {
	private $errorList = array();
	function __construct($registry)
	{
		parent::__construct($registry);
		$this->errorList['username']="";
		$this->errorList['password']="";
	}
	public function get(){
		return $this->registry;
	}
	public function index(/*$user*/)
	{
		$method = $_SERVER['REQUEST_METHOD'];
		if (isset($_SESSION['username']) || isset($_COOKIE['username'])) {
			header("Location: ./index");
			exit();
		}
		/*if ($this->registry->db->get('userManager')->getCurrentUser()) 
		{
			//var_export($this->registry->db->get('userManager')->getCurrentUser());
			header("Location: ..");
			//echo($this->registry->db->get('userManager')->getCurrentUser()->get('fbid'));
			exit();
		}*/
		
		else if ($method=='POST'){
			$username = $_POST['username'];
			$pass = $_POST['password'];
			
			$usermana = $this->registry->db->get('userManager');

			try {
				if ($usermana->checkUserPass2($username, $pass)) 
				{
					if($usermana->checkAuth($username)) {
						$usermana->login($username, $pass);
						header("Location: ./index");
						exit();
					}
					else {
						$this->errorList['username'] = "Username lacks CREATE SESSION privilege.<br/> Logon denied <br>";
					}
				}
				else
				{
					$this->errorList['password'] = "Password or Username isn't correct.";
				}
			}
			catch (Exception $error) {
				$this->errorList['username'] = "Username isn't correct or locked.<br>";
			};
		}
		$content = $this->registry->template->getTemplate('login', $this->errorList, 'View');
		echo $content;
	}
}
?>